<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Service\User;

use App\Models\User;
use App\Models\WalletAccount;
use App\Models\WalletTransaction;
use App\Service\BaseService;
use App\Service\Notifications\NotificationService;

class WalletAccountService extends BaseService
{
    /**
     * 查询余额.
     * @param mixed $user_id
     */
    public function checkTheBalance($user_id): float
    {
        $account = WalletAccount::query()->where('user_id', $user_id)->first();
        return $account->balance;
    }

    /**
     * 充值
     * @param mixed $user_id
     * @param mixed $amount
     * @param mixed $describe
     * @param mixed $is_refund
     * @param mixed $transaction_type
     * @return false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|void
     */
    public function recharge($user_id, $amount, $describe = '充值', $is_refund = false, $transaction_type = 1)
    {
        if ($amount < 0) {
            return false;
        }
        if ($describe == '充值') {
            $describe = '充值:' . $amount;
        }
        $account = WalletAccount::query()->where('user_id', $user_id)->first();
        if (! $is_refund) {
            $account->total_wallet += $amount;
        }
        $account->balance += $amount;
        $account->save();
        $this->transactionHistory($user_id, $amount, $account->balance, $transaction_type, $describe, 1);
        return $account;
    }

    /**
     * 消费.
     *
     * @param mixed $user_id
     * @param mixed $amount
     * @param mixed $describe
     * @param mixed $transaction_type
     * @return false|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|void
     */
    public function consumption($user_id, $amount, $describe = '消费', $transaction_type = 2)
    {
        $account = WalletAccount::query()->where('user_id', $user_id)->first();
        if ($account->balance - $amount < 0) {
            return false;
        }
        if ($describe == '消费') {
            $describe = '消费:' . $amount;
        }
        $account->balance -= $amount;
        $account->save();
        $this->transactionHistory($user_id, $amount, $account->balance, $transaction_type, $describe, 2);
        return $account;
    }

//    是否足够
    public function isItSufficient($user_id, $amount): bool
    {
        $account = WalletAccount::query()->where('user_id', $user_id)->first();
        if ($account->balance - $amount < 0) {
            return false;
        }
        return true;
    }

    /**
     * 交易记录.
     * @param mixed $user_id
     * @param mixed $amount
     * @param mixed $balance
     * @param mixed $transaction_type
     * @param mixed $describe
     * @param mixed $change_type
     */
    private function transactionHistory($user_id, $amount, $balance, $transaction_type, $describe, $change_type)
    {
        WalletTransaction::query()->create([
            'user_id' => $user_id,
            'amount' => $amount,
            'balance' => $balance,
            'transaction_type' => $transaction_type,
            'describe' => $describe,
            'status' => 2,
            'change_type' => $change_type,
        ]);
//        $user = User::query()->find($user_id);
//        if (filled($user)) {
//            NotificationService::make()->SystemNotify($user, $describe);
//        }
    }
}
